﻿@namespace Bit.BlazorUI
@inherits BitComponentBase

<div @ref="RootElement" @attributes="HtmlAttributes"
     id="@_Id"
     style="@StyleBuilder.Value"
     class="@ClassBuilder.Value"
     dir="@Dir?.ToString().ToLower()">
    @if (ShowFirstButton)
    {
        <button @onclick="@(() => ChangePage(1))"
                aria-label="First page"
                disabled="@(SelectedPage == 1 || IsEnabled is false)"
                style="@Styles?.Button @Styles?.FirstButton"
                class="bit-pgn-btn @Classes?.Button @Classes?.FirstButton @GetButtonClasses()">
            <i style="@Styles?.FirstButtonIcon" class="bit-icon bit-icon--@(FirstIcon ?? "ChevronRightEnd6 bit-pgn-trs") @Styles?.FirstButtonIcon" />
        </button>
    }

    @if (ShowPreviousButton)
    {
        <button @onclick="@(() => ChangePage(SelectedPage - 1))"
                aria-label="Previous page"
                disabled="@(SelectedPage == 1 || IsEnabled is false)"
                style="@Styles?.Button @Styles?.PreviousButton"
                class="bit-pgn-btn @Classes?.Button @Classes?.PreviousButton @GetButtonClasses()">
            <i style="@Styles?.PreviousButtonIcon" class="bit-icon bit-icon--@(PreviousIcon ?? "ChevronRight bit-pgn-trs") @Classes?.PreviousButtonIcon" />
        </button>
    }

    @foreach (var page in GeneratePages())
    {
        var currentPage = page;
        if (currentPage == -1)
        {
            <div style="@Styles?.Ellipsis" class="bit-pgn-elp @Classes?.Ellipsis" disabled="@(IsEnabled is false)">&bull;&bull;&bull;</div>
        }
        else if (currentPage == SelectedPage)
        {
            <button disabled="@(IsEnabled is false)"
                    aria-current="page"
                    aria-label="@($"Current page {currentPage}")"
                    style="@Styles?.Button @Styles?.SelectedButton"
                    class="bit-pgn-btn bit-pgn-sel @Classes?.Button @Classes?.SelectedButton @GetButtonClasses()">
                @currentPage
            </button>
        }
        else
        {
            <button @onclick="@(() => ChangePage(currentPage))"
                    disabled="@(IsEnabled is false)"
                    aria-label="@($"Page {currentPage}")"
                    style="@Styles?.Button"
                    class="bit-pgn-btn @Classes?.Button @GetButtonClasses()">
                @currentPage
            </button>
        }
    }

    @if (ShowNextButton)
    {
        <button @onclick="@(() => ChangePage(SelectedPage + 1))"
                aria-label="Next page"
                disabled="@(SelectedPage == Count || IsEnabled is false)"
                style="@Styles?.Button @Styles?.NextButton"
                class="bit-pgn-btn @Classes?.Button @Classes?.NextButton @GetButtonClasses()">
            <i style="@Styles?.NextButtonIcon" class="bit-icon bit-icon--@(NextIcon ?? "ChevronRight bit-pgn-tre") @Classes?.NextButtonIcon" />
        </button>
    }

    @if (ShowLastButton)
    {
        <button @onclick="@(() => ChangePage(Count))"
                aria-label="Last page"
                disabled="@(SelectedPage == Count || IsEnabled is false)"
                style="@Styles?.Button @Styles?.LastButton"
                class="bit-pgn-btn @Classes?.Button @Classes?.LastButton @GetButtonClasses()">
            <i style="@Styles?.LastButtonIcon" class="bit-icon bit-icon--@(LastIcon ?? "ChevronRightEnd6 bit-pgn-tre") @Classes?.LastButtonIcon" />
        </button>
    }
</div>